using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ctsu.BaseForms;

using ctsu.Data;

namespace ctsu.Admin.Forms
{
	/// <summary>
	/// Summary description for NewPatientDlg.
	/// </summary>
	public class NewPatientDlg : System.Windows.Forms.Form
	{
		public FoxPatientDisplay NewPatient;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox PatientIdentifier;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox LastName;
		private System.Windows.Forms.TextBox FirstName;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnCancel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public NewPatientDlg()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(NewPatientDlg));
			this.label1 = new System.Windows.Forms.Label();
			this.PatientIdentifier = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.LastName = new System.Windows.Forms.TextBox();
			this.FirstName = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Patient Identifier: ";
			// 
			// PatientIdentifier
			// 
			this.PatientIdentifier.Location = new System.Drawing.Point(104, 16);
			this.PatientIdentifier.Name = "PatientIdentifier";
			this.PatientIdentifier.Size = new System.Drawing.Size(160, 20);
			this.PatientIdentifier.TabIndex = 1;
			this.PatientIdentifier.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Last Name:";
			// 
			// LastName
			// 
			this.LastName.Location = new System.Drawing.Point(104, 48);
			this.LastName.Name = "LastName";
			this.LastName.Size = new System.Drawing.Size(128, 20);
			this.LastName.TabIndex = 3;
			this.LastName.Text = "";
			// 
			// FirstName
			// 
			this.FirstName.Location = new System.Drawing.Point(104, 80);
			this.FirstName.Name = "FirstName";
			this.FirstName.Size = new System.Drawing.Size(128, 20);
			this.FirstName.TabIndex = 5;
			this.FirstName.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 80);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(80, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "First Name:";
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(59, 112);
			this.btnSave.Name = "btnSave";
			this.btnSave.TabIndex = 6;
			this.btnSave.Text = "&Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(147, 112);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 7;
			this.btnCancel.Text = "&Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// NewPatientDlg
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(280, 142);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.FirstName);
			this.Controls.Add(this.LastName);
			this.Controls.Add(this.PatientIdentifier);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "NewPatientDlg";
			this.Text = "New Patient";
			this.ResumeLayout(false);

		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			StringBuilder sb = new StringBuilder();
			if (PatientIdentifier.Text.Trim().Length == 0)
			{
				MessageBox.Show(this, "Patient Identifier is a required field", "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				PatientIdentifier.Focus();
				return;
			}

			if (IsDuplicate())
				return;

			//SqlConnection conn = LoginForm.GetSqlConnection();
			string sql = "INSERT INTO PatientsNew (MRN, LastName, FirstName) " +
				" VALUES ('" + PatientIdentifier.Text.Trim() + "'" +
				" , '" + LastName.Text + "'" +
				", '" + FirstName.Text + "')";
			try
			{
				DataAccess.ExecuteNonQuery(CommandType.Text, sql);

				string name = "";
				if (LastName.Text.Trim().Length > 0)
					name = LastName.Text.Trim() + ", ";
				if (FirstName.Text.Trim().Length > 0)
					name += FirstName.Text.Trim();

				NewPatient = new FoxPatientDisplay(PatientIdentifier.Text, name);
				MessageBox.Show(this, "New patient was saved successfully", "Confirm Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
				this.Close();
			}
			catch (Exception ex)
			{
				////ExceptionPolicy.HandleException(ex, "Exception Policy");
				sb.Append("Could not save new patient.");
				sb.Append(Environment.NewLine);
				sb.Append("Error: " + ex.Message);
				MessageBox.Show(this, sb.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			finally
			{
				//if(conn != null)
				//    conn.Dispose();
			}
		}

		private bool IsDuplicate()
		{
			StringBuilder sb = new StringBuilder();

			//SqlConnection conn = LoginForm.GetSqlConnection();
			string sql = "SELECT MRN FROM PatientsNew " +
				" WHERE MRN ='" + PatientIdentifier.Text + "'";

			IDataReader reader = null;
			try
			{
				reader = DataAccess.GetDataReader(sql);

				if (reader.Read())
				{
					MessageBox.Show(this, "This Patient Identifier is currently in the Patients table.  Duplicate identifiers are not allowed.", "Duplicate Identifier", MessageBoxButtons.OK, MessageBoxIcon.Stop);
					PatientIdentifier.Focus();
					return true;
				}

			}
			catch (Exception ex)
			{
				////ExceptionPolicy.HandleException(ex, "Exception Policy");
				sb.Append("Encountered an error while checking for duplicate patient identifier.");
				sb.Append(Environment.NewLine);
				sb.Append("Error: " + ex.Message);
				MessageBox.Show(this, sb.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return true;
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
			}

			return false;
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
